package com.bhs.sansonglogistics.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.LogisticsTrackData;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.LogisticsTrackAdapter;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 物流轨迹
 */
public class LogisticsTrackDialog extends Dialog implements View.OnClickListener, NetCallBack {


    private TextView mTvWaybillNumber;
    private TextView mTvCopy2;
    private FrameLayout mFlClose;
    private RecyclerView mRvList;
    private BaseActivity activity;
    private LogisticsTrackAdapter logisticsTrackAdapter;
    private String deliver_sn;
    private MyClickListener listener;
    public LogisticsTrackDialog(@NonNull BaseActivity context, String deliver_sn) {
        super(context, R.style.MyBottomDialogStyle);
        this.activity = context;
        this.deliver_sn = deliver_sn;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_logistics_track);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.dimAmount = 0f;
        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = MyUtils.getScreenHeight(getContext()) * 7 / 10;
        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
        activity.networkRequest(activity.netApi.get_order_logistics(deliver_sn), this, 666);
        mTvWaybillNumber.setText(String.format("订单号：%s", deliver_sn));

    }

    private void initView() {
        mTvWaybillNumber = findViewById(R.id.tv_waybill_number);
        mTvCopy2 = findViewById(R.id.tv_copy2);
        mTvCopy2.setOnClickListener(this);
        mFlClose = findViewById(R.id.fl_close);
        mFlClose.setOnClickListener(this);
        mRvList = findViewById(R.id.rv_list);
        logisticsTrackAdapter = new LogisticsTrackAdapter();
        mRvList.setAdapter(logisticsTrackAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
    }
    public void setMyClickListener(@Nullable MyClickListener l) {
        this.listener = l;
    }
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_copy2) {
            listener.onClick(view);
        } else {
            dismiss();
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        LogisticsTrackData trackData = new Gson().fromJson(json, LogisticsTrackData.class);
        if (trackData.isStatus()) {
            logisticsTrackAdapter.setNewData(trackData.getData().getOrder_logistics_list());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}
